package com.bs.resa.web;

import com.bs.resa.common.DatetimeUtils;
import com.bs.resa.common.RedisUtils;
import com.bs.resa.pojo.AdminUser;
import com.bs.resa.pojo.Module;
import com.bs.resa.pojo.PType;
import com.bs.resa.pojo.Project;
import com.bs.resa.service.AdminUserService;
import com.bs.resa.service.ModuleService;
import com.bs.resa.service.ProjectService;
import com.bs.resa.service.ProjectTypeService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author lin
 * @date 2019/3/22 11:37
 */
@Controller
public class ProjectController {
    private static Logger logger = LoggerFactory.getLogger(ProjectController.class);
    protected Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
    @Resource
    RedisUtils redisUtils;

    @Autowired
    ProjectTypeService projectTypeService;
    @Autowired
    ModuleService moduleService;
    @Autowired
    ProjectService projectService;
    @Autowired
    AdminUserService adminUserService;

    @ApiOperation(value = "查询所有项目类型", notes = "POST")
    @ResponseBody
    @RequestMapping(value = "project/pTypeQuery.do", method = RequestMethod.POST)
    public Object projectTypeQuery() {
        List<PType> pTypes = projectTypeService.getProjectTypeList();
        logger.info("项目类型：" + pTypes);
        List<Module> modulesList = moduleService.getModuleList();
        logger.info("项目组件：" + modulesList);
        resultMap.put("PType", pTypes);
        resultMap.put("Module", modulesList);
        return resultMap;
    }


    @ApiOperation(value = "创建项目", notes = "POST")
    @ResponseBody
    @RequestMapping(value = "project/projectCreateSum.do", method = RequestMethod.POST)
    public Object projectCreateSum(
            String iId,
            String m,
            String pname,
            String pnumber,
            String ptaskpre,
            String description,
            Date endDate,
            String puserid) throws UnsupportedEncodingException, ParseException {
        Project project = new Project();

        logger.info(iId);
        logger.info(m);
        logger.info(pname + "/" + pnumber + "/" + ptaskpre + "/" + description);
        AdminUser adminUser = (AdminUser) redisUtils.getObject("userinfo");
        logger.info(adminUser.getAccount());
        project.setPname(pname);  //项目名称
        project.setPnumber(pnumber);  //项目编号
        project.setPtaskpre(ptaskpre); //项目任务编号前缀
        project.setDescription(description);  //项目描述
        project.setPtypeid(Long.valueOf(iId));  //项目类型id
        project.setPstatusid(Long.valueOf(1));  //项目默认状态：1未开始，2进行中，3已完成
        project.setPcrateUserid(adminUser.getId());//创建人id
        logger.info("puserid：" + puserid);
        project.setPuserid(Long.valueOf(puserid));
        System.out.println("data:" + endDate);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        project.setPendDate(df.parse(df.format(endDate)));
        project.setPcreatedate(df.parse(df.format(new Date())));
        try {
            //删除项目id的redis
//            String pid=redisUtils.get("pid");
            //添加项目
            Integer i = projectService.insertProject(project);
            //添加项目id的 redis
            redisUtils.set("pid", i + "");

            logger.info("项目id:" + i);
            //项目主键
            String[] module = m.split("/");
            List<Integer> mid = new ArrayList<>();
            for (String s : module) {
//                System.out.println(s);
                if (!s.equals("0") && Integer.parseInt(s) != 0) {
                    logger.info("添加组件");
                    mid.add(Integer.valueOf(s));
                }
            }
//            System.out.println(mid);
            //添加项目-组件关系表记录
            logger.info("添加相关组件");
            if (mid != null && mid.size() > 0) {
                moduleService.insertProjectModule(i, mid);
            }

            resultMap.put("code", 200);
            resultMap.put("message", "add project success");
        } catch (Exception e) {
            resultMap.put("code", 500);
            resultMap.put("message", e.getMessage());
        }


        return resultMap;
    }


    @ApiOperation(value = "项目概述图表数据", notes = "POST")
    @ResponseBody
    @RequestMapping(value = "project/task/detail/{pid}.do", method = RequestMethod.POST)
    public Object getDeatilDate(@PathVariable("pid") String pid) {
        Map<String, Object> map = projectService.getDetail(pid);

        return map;
    }


    @ApiOperation(value = "修改项目基本信息", notes = "POST")
    @ResponseBody
    @RequestMapping(value = "project/updataInfo/{pid}.do", method = RequestMethod.POST)
    public Object updateProjectInfo(Project project, @PathVariable("pid") String pid) {

        try {
            boolean flag = projectService.updataProjectInfo(project, pid);

            if (flag) {
                resultMap.put("code", 200);
                System.out.println("修改项目基本信息");
                System.out.println(project.toString());
                resultMap.put("message", "add project success");
                resultMap.put("data", project);
            }


        } catch (ParseException e) {
            e.printStackTrace();
        }

        return resultMap;
    }


    @ApiOperation(value = "修改项目参与人员", notes = "POST")
    @ResponseBody
    @RequestMapping(value = "project/updateProjectUsers.do", method = RequestMethod.POST)
    public Object updateProjectInfo(String data) {
        projectService.updateProjectUser(data);
        return resultMap;
    }

    @ApiOperation(value = "查询项目参与人", notes = "POST")
    @ResponseBody
    @RequestMapping(value = "project/queryProjectAdmin.do", method = RequestMethod.POST)
    public Object queryProjectAdmin() {
        List<AdminUser> admin=adminUserService.queryByPid(redisUtils.get("pid"));

        return admin;
    }
}